package com.xbongbong.paas.controller.mobile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.pojo.dto.FormDataGetDTO;
import com.xbongbong.paas.pojo.vo.FormDataAddVO;
import com.xbongbong.paas.pojo.vo.FormDataDeleteBatchVO;
import com.xbongbong.paas.pojo.vo.FormDataGetVO;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.thread.TransmittableThreadLocalUtil;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.knowledge.base.pojo.dto.KnowledgeAddDTO;
import com.xbongbong.pro.knowledge.base.pojo.dto.KnowledgeBaseDetailDTO;
import com.xbongbong.pro.knowledge.base.pojo.dto.KnowledgeCatalogueNameLikeDTO;
import com.xbongbong.pro.knowledge.base.pojo.dto.KnowledgeOperationDTO;
import com.xbongbong.pro.knowledge.base.pojo.dto.TagScreenListDTO;
import com.xbongbong.pro.knowledge.base.pojo.vo.KnowledgeCatalogueListVO;
import com.xbongbong.pro.knowledge.base.pojo.vo.TagScreenListVO;
import com.xbongbong.saas.service.KnowledgeBaseService;
import com.xbongbong.saas.service.KnowledgeDataService;
import com.xbongbong.saas.service.KnowledgeTagService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author mingliang.cao
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/mobile/knowledge")
public class MobileKnowledgeBaseController {
    @Resource
    private KnowledgeBaseService knowledgeBaseService;
    @Resource
    private KnowledgeDataService knowledgeDataService;
    @Resource
    private PaasRedisHelper paasRedisHelper;
    @Resource
    private KnowledgeTagService knowledgeTagService;


    /**
     * @Author mingliang
     * @Description 移动端目录树结构
     * @Date 2022/6/30 10:12
     * @param knowledgeBaseDetailDTO
     * @param br
     **/
    @RequestMapping(value = "/catalogue/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String knowledgeCatalogueList(@RequestBody @Valid KnowledgeBaseDetailDTO knowledgeBaseDetailDTO, BindingResult br) throws Exception {
        XbbResponse<KnowledgeCatalogueListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                KnowledgeCatalogueListVO knowledgeCatalogueListVO = knowledgeBaseService.knowledgeCatalogueList(knowledgeBaseDetailDTO);
                response = new XbbResponse<>(knowledgeCatalogueListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * @Author mingliang
     * @Description 知识库目录名称模糊查询
     * @Date 2022/6/30 10:14
     * @param knowledgeCatalogueNameLikeDTO
     * @param br
     **/
    @RequestMapping(value = "/catalogue/nameLikeQuery", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String knowledgeCatalogueNameLikeQuery(@RequestBody @Valid KnowledgeCatalogueNameLikeDTO knowledgeCatalogueNameLikeDTO, BindingResult br) throws Exception {
        XbbResponse<KnowledgeCatalogueListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                KnowledgeCatalogueListVO knowledgeCatalogueListVO = knowledgeBaseService.knowledgeCatalogueNameLikeQuery(knowledgeCatalogueNameLikeDTO);
                response = new XbbResponse<>(knowledgeCatalogueListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }


    /**
     * 获取知识条目数据详情
     * @param formDataGetDTO
     * @param br
     * @returnadd
     */
    @RequestMapping(value = "/dataDetail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String dataDetail(@RequestBody @Valid FormDataGetDTO formDataGetDTO, BindingResult br) {
        XbbResponse<FormDataGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(formDataGetDTO.getSaasMark(), formDataGetDTO.getBusinessType(), formDataGetDTO.getMenuId(), formDataGetDTO.getDistributorMark());
                FormDataGetVO formDataGetVO = knowledgeDataService.appKnowledgeDetail(formDataGetDTO);
                response = new XbbResponse<>(formDataGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 批量删除数据
     * @param knowledgeOperationDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delete(@RequestBody @Valid KnowledgeOperationDTO knowledgeOperationDTO, BindingResult br) {
        XbbResponse<FormDataDeleteBatchVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(knowledgeOperationDTO.getSaasMark(), knowledgeOperationDTO.getBusinessType(), knowledgeOperationDTO.getMenuId(), knowledgeOperationDTO.getDistributorMark());
                FormDataDeleteBatchVO formDataDeleteBatchVO = knowledgeDataService.deleteBatch(knowledgeOperationDTO);
                //FormDataDeleteBatchVO formDataDeleteBatchVO = listBatchService.deleteBatch(formDataDeleteBatchDTO);
                response = new XbbResponse<>(formDataDeleteBatchVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }


    /**
     * 停用/启用知识条目
     * @param knowledgeOperationDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/setUp", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String setUp(@RequestBody @Valid KnowledgeOperationDTO knowledgeOperationDTO, BindingResult br) {
        XbbResponse<FormDataDeleteBatchVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(knowledgeOperationDTO.getSaasMark(), knowledgeOperationDTO.getBusinessType(), knowledgeOperationDTO.getMenuId(), knowledgeOperationDTO.getDistributorMark());
                FormDataDeleteBatchVO formDataDeleteBatchVO = knowledgeDataService.setupKnowledge(knowledgeOperationDTO);
                //FormDataDeleteBatchVO formDataDeleteBatchVO = listBatchService.deleteBatch(formDataDeleteBatchDTO);
                response = new XbbResponse<>(formDataDeleteBatchVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }


    @RequestMapping("/add")
    public String addKnowledge(@RequestBody @Valid KnowledgeAddDTO knowledgeAddDTO, BindingResult br) {
        XbbResponse<FormDataAddVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                //重复提交校验
                if (!paasRedisHelper.checkConcurrentLock(knowledgeAddDTO)) {
                    response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                    return JSON.toJSONString(response);
                }
                TransmittableThreadLocalUtil.setValue(knowledgeAddDTO.getSaasMark(), knowledgeAddDTO.getBusinessType(), knowledgeAddDTO.getMenuId(), knowledgeAddDTO.getDistributorMark());
                FormDataAddVO formDataSaveVO= knowledgeDataService.addKnowledge(knowledgeAddDTO);
                response = new XbbResponse<>(formDataSaveVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }


    /**
     * @Author mingliang
     * @Description
     * @Date 2022/7/5 14:35
     * @param tagScreenListDTO
     * @param br
     **/
    @RequestMapping(value = "/tagList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String knowledgeTagList(@RequestBody @Valid TagScreenListDTO tagScreenListDTO, BindingResult br) {
        XbbResponse<TagScreenListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TagScreenListVO tagScreenListVO = knowledgeTagService.getScreenList(tagScreenListDTO);
                response = new XbbResponse<>(tagScreenListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
